#include "StdAfx.h"
#include "DrawDrawing.h"


CDrawDrawing::CDrawDrawing(void)
{
	line_type = CV_AA; // change it to 8 to see non-antialiased graphics
    ymin = 0;
	i = 0;
}


CDrawDrawing::~CDrawDrawing(void)
{
}
BEGIN_MESSAGE_MAP(CDrawDrawing, CDrawOpenCV)
	ON_WM_PAINT()
END_MESSAGE_MAP()


CvScalar CDrawDrawing::RandomColor(CvRNG* rng)
{
    int icolor = cvRandInt(rng);
    return CV_RGB(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
}


void CDrawDrawing::DealwithFrame()
{
	// TODO: Add your command handler code here
	if(m_pFrame == NULL)
	{			
		return ;
	}
	if(m_pShow == NULL)
	{
		cvReleaseImage(&m_pShow);		
		width = m_pFrame->width, height = m_pFrame->height;
		width3 = width*3, height3 = height*3;
		m_pShow = cvCreateImage( cvSize(width,height), 8, 3 );

		rng = cvRNG((unsigned)-1);
		pt[0] = &(ptt[0]);
		pt[1] = &(ptt[3]);

		arr[0] = 3;
		arr[1] = 3;
	}

    // Load the source image
 //   IplImage* image = cvCreateImage( cvSize(width,height), 8, 3 );
 //   IplImage* image2;

 //   // Create a window
 //   cvNamedWindow(wndnamed, 1 );
 //   cvZero( image );
 //   cvShowImage(wndnamed,image);


	//m_ImageType=-3;
	++i;

    if (i< 100)
    {
        pt1.x=cvRandInt(&rng) % width3 - width;
        pt1.y=cvRandInt(&rng) % height3 - height;
        pt2.x=cvRandInt(&rng) % width3 - width;
        pt2.y=cvRandInt(&rng) % height3 - height;
        cvLine( m_pShow, pt1, pt2, RandomColor(&rng), cvRandInt(&rng)%10, line_type, 0 );
    }
	else if(i<200)
    {
        pt1.x=cvRandInt(&rng) % width3 - width;
        pt1.y=cvRandInt(&rng) % height3 - height;
        pt2.x=cvRandInt(&rng) % width3 - width;
        pt2.y=cvRandInt(&rng) % height3 - height;
        cvRectangle( m_pShow,pt1, pt2, RandomColor(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
    }
	else if(i<300)
    {
		pt1.x=cvRandInt(&rng) % width3 - width;
		pt1.y=cvRandInt(&rng) % height3 - height;
		sz.width =cvRandInt(&rng)%200;
		sz.height=cvRandInt(&rng)%200;
		angle = (cvRandInt(&rng)%1000)*0.180;
		cvEllipse( m_pShow, pt1, sz, angle, angle - 100, angle + 200,
					RandomColor(&rng), cvRandInt(&rng)%10-1, line_type, 0 );
	}
	else if(i<400)
    {
        pt[0][0].x=cvRandInt(&rng) % width3 - width;
        pt[0][0].y=cvRandInt(&rng) % height3 - height;
        pt[0][1].x=cvRandInt(&rng) % width3 - width;
        pt[0][1].y=cvRandInt(&rng) % height3 - height;
        pt[0][2].x=cvRandInt(&rng) % width3 - width;
        pt[0][2].y=cvRandInt(&rng) % height3 - height;
        pt[1][0].x=cvRandInt(&rng) % width3 - width;
        pt[1][0].y=cvRandInt(&rng) % height3 - height;
        pt[1][1].x=cvRandInt(&rng) % width3 - width;
        pt[1][1].y=cvRandInt(&rng) % height3 - height;
        pt[1][2].x=cvRandInt(&rng) % width3 - width;
        pt[1][2].y=cvRandInt(&rng) % height3 - height;
        cvPolyLine( m_pShow, pt, arr, 2, 1, RandomColor(&rng), cvRandInt(&rng)%10, line_type, 0 );
	}
	else if(i<500)
    {
        pt[0][0].x=cvRandInt(&rng) % width3 - width;
        pt[0][0].y=cvRandInt(&rng) % height3 - height;
        pt[0][1].x=cvRandInt(&rng) % width3 - width;
        pt[0][1].y=cvRandInt(&rng) % height3 - height;
        pt[0][2].x=cvRandInt(&rng) % width3 - width;
        pt[0][2].y=cvRandInt(&rng) % height3 - height;
        pt[1][0].x=cvRandInt(&rng) % width3 - width;
        pt[1][0].y=cvRandInt(&rng) % height3 - height;
        pt[1][1].x=cvRandInt(&rng) % width3 - width;
        pt[1][1].y=cvRandInt(&rng) % height3 - height;
        pt[1][2].x=cvRandInt(&rng) % width3 - width;
        pt[1][2].y=cvRandInt(&rng) % height3 - height;

        cvFillPoly( m_pShow, pt, arr, 2, RandomColor(&rng), line_type, 0 );
	}
	else if(i<600)
    {
        pt1.x=cvRandInt(&rng) % width3 - width;
        pt1.y=cvRandInt(&rng) % height3 - height;
        cvCircle( m_pShow, pt1, cvRandInt(&rng)%300, RandomColor(&rng),
                  cvRandInt(&rng)%10-1, line_type, 0 );
	}
	else if(i<700)
    {
        pt1.x=cvRandInt(&rng) % width3 - width;
        pt1.y=cvRandInt(&rng) % height3 - height;
        cvInitFont( &font, cvRandInt(&rng) % 8,
			(cvRandInt(&rng)%100)*0.05+0.1, (cvRandInt(&rng)%100)*0.05+0.1, 
			(cvRandInt(&rng)%5)*0.1, cvRound(cvRandInt(&rng)%10), line_type );

        cvPutText( m_pShow, "Testing text rendering!", pt1, &font, RandomColor(&rng));
	}
	else  if(i<800)
    {
		cvInitFont( &font, CV_FONT_HERSHEY_COMPLEX, 3, 3, 0.0, 5, line_type );
		cvGetTextSize( "OpenCV forever!", &font, &text_size, &ymin );
		pt1.x = (width - text_size.width)/2;
		pt1.y = (height + text_size.height)/2;
		IplImage* image2;
		image2 = cvCloneImage(m_pShow);
		for( i = 0; i < 255; i++ )
		{
			cvSubS( image2, cvScalarAll(i), m_pShow, 0 );
			cvPutText( m_pShow, "OpenCV forever!", pt1, &font, CV_RGB(255,i,i));
		}
		cvReleaseImage(&image2);
	}
	else
	{
		if(m_pShow != NULL)
		{
			cvReleaseImage(&m_pShow);		
			width = m_pFrame->width, height = m_pFrame->height;
			width3 = width*3, height3 = height*3;
			m_pShow = cvCreateImage( cvSize(width,height), 8, 3 );
		}
		rng = cvRNG((unsigned)-1);
		pt[0] = &(ptt[0]);
		pt[1] = &(ptt[3]);
		i = 0;
		arr[0] = 3;
		arr[1] = 3;
	}

}
